#include <extra/rgView.h>



rg::view::view(const rgInt& xRes, const rgInt& yRes, const rgBool gl )
{
	//initialize systems
	m_xRes = xRes;
	m_yRes = yRes;
	//set our at exit function
	//atexit ( SDL_Quit ) ;
	//create a window
	rgUint flags;
	if (gl)
	{
		flags = SDL_OPENGL;// | SDL_FULLSCREEN;
	}
	else
	{
		#ifdef NDEBUG
			flags = SDL_DOUBLEBUF | SDL_HWSURFACE;//| SDL_FULLSCREEN;
		#else
			flags = SDL_DOUBLEBUF | SDL_HWSURFACE;
		#endif
	}

	m_pSurface = SDL_SetVideoMode ( xRes , yRes , 0, flags ) ;
	m_black = SDL_MapRGB(m_pSurface->format, 0, 0, 0);
	m_pitch = m_pSurface->pitch/4;
	
	SDL_ShowCursor(0);

	TTF_Init();
	m_fntCourier = TTF_OpenFont( "../Fonts/cour.ttf", 14 );
    
	
	
	m_fullScreen.h = (rgUint16)yRes;
	m_fullScreen.w = (rgUint16)xRes;
	m_fullScreen.x = 0;
	m_fullScreen.y = 0;
	clear();
}

void rg::view::print( const rgUint16& x, const rgUint16& y, const string& str )
{
	SDL_Color clrFg = {255, 255, 0, 0};  
	SDL_Surface *sText = TTF_RenderText_Solid( m_fntCourier, str.c_str(), clrFg );
    SDL_Rect rcDest = {x, y,0,0};
    SDL_BlitSurface( sText, NULL, m_pSurface,&rcDest );
	SDL_FreeSurface( sText );

}

void rg::view::drawPoint( const rgUint16& x, const rgUint16& y, const rgUint16& w, const rgUint16& h, const rgUint32& color )
{
    m_point.h = h;
	m_point.w = w;
	m_point.x = x;
	m_point.y = (Sint16)(m_yRes - y);
	//m_point = (SDL_Rect) {x, y, 1, 1};
    SDL_FillRect(m_pSurface, &m_point, color);
}

void rg::view::LockSurface()
{
    if ( SDL_MUSTLOCK(m_pSurface) ) 
    {
        if ( SDL_LockSurface(m_pSurface) < 0 ) 
	{
            return;
        }
    }
}

void rg::view::UnLockSurface()
{
    if ( SDL_MUSTLOCK(m_pSurface) ) 
    {
        SDL_UnlockSurface(m_pSurface);
    }
	//SDL_UpdateRect(m_pSurface, 0, 0, m_xRes, m_yRes);
}

void rg::view::DrawPixel2(const rgUint32& color, const rgUint32& x, const rgUint32& y)
{
    Uint32 *bufp;
    bufp = static_cast<rgUint32*>(m_pSurface->pixels) + y*m_pitch + x;
    *bufp = color;
}

void rg::view::DrawPixel(Uint8 R, Uint8 G, Uint8 B, Uint32 x, Uint32 y)
{
    Uint32 color = SDL_MapRGB(m_pSurface->format, R, G, B);

    if ( SDL_MUSTLOCK(m_pSurface) ) 
    {
        if ( SDL_LockSurface(m_pSurface) < 0 ) 
	{
            return;
        }
    }
    switch (m_pSurface->format->BytesPerPixel) 
    {    
        case 1: 
	{ // Asumimos 8-bpp 
            Uint8 *bufp;

            bufp = (Uint8 *)m_pSurface->pixels + y*m_pSurface->pitch + x;
            *bufp = (Uint8)color;
        }
        break;

        case 2: 
	{ // Probablemente 15-bpp o 16-bpp 
            Uint16 *bufp;

            bufp = (Uint16 *)m_pSurface->pixels + y*m_pSurface->pitch/2 + x;
            *bufp = (Uint16)color;
        }
        break;

        case 3: 
	{ // Modo lento 24-bpp, normalmente no usado 
            Uint8 *bufp;

            bufp = (Uint8 *)m_pSurface->pixels + y*m_pSurface->pitch + x;
            *(bufp+m_pSurface->format->Rshift/8) = R;
            *(bufp+m_pSurface->format->Gshift/8) = G;
            *(bufp+m_pSurface->format->Bshift/8) = B;
        }
        break;

        case 4: 
	{ // Probablemente 32-bpp 
            Uint32 *bufp;

            bufp = (Uint32 *)m_pSurface->pixels + y*m_pSurface->pitch/4 + x;
            *bufp = color;
        }
        break;
    }
    if ( SDL_MUSTLOCK(m_pSurface) ) 
    {
        SDL_UnlockSurface(m_pSurface);
    }
    SDL_UpdateRect(m_pSurface, x, y, 1, 1);
}

rg::view::~view(void)
{
	TTF_CloseFont( m_fntCourier );
}
